package ca.inrs.ete.app.shopx.images;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import ca.inrs.ete.app.shopx.MainMenu;
import ca.inrs.ete.app.shopx.R;
import ca.inrs.ete.app.shopx.util.Constants;


public class ImagePagerActivity extends FragmentActivity {

	private Menu       mMainMenu = null;
	private ViewPager  mPager = null;
	private ImagePagerAdapter mPagerAdapter = null;

	private int        mNbImages = 0;
	private Uri[]      mImages;

	public static class PagerDefault extends ImagePagerActivity {}
	public static class PagerWithID  extends ImagePagerActivity {}

	/**
	 * ------------------------------------------------------------------------------
	 * 	
	 * ------------------------------------------------------------------------------
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// ---  Extract intent information
		final Intent intent = getIntent();
		Uri folderUri = intent.getData();
		int indx = intent.getIntExtra(Constants.INTENT_EXTRA_INT, 0);
		
		// ---  Get the images list
		mImages = getImagesFiles(folderUri);
		mNbImages = mImages.length;

		// ---   Layout
		setContentView(R.layout.activity_image_pager);
		getActionBar().setDisplayHomeAsUpEnabled(true);

		// ---   Setup pager and adapter
		mPager = (ViewPager) findViewById(R.id.image_pager);
		mPagerAdapter = new ImagePagerAdapter( getSupportFragmentManager() );
		mPager.setAdapter(mPagerAdapter);
		mPager.setOffscreenPageLimit(2);
		mPager.setCurrentItem(indx);

		// ---   Start
		notifyPagerDataChange();
	}

	@Override
	protected void onStart(){
		super.onStop();
	}

	@Override
	protected void onResume() {
	    super.onResume();
	}

	@Override
	protected void onPause() {
	    super.onPause();
	}

	@Override
	protected void onStop(){
		super.onStop();
	}

	@Override
	protected void onDestroy() {
	    super.onPause();
	    // remember current page
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_image_pager, menu);
		getMenuInflater().inflate(R.menu.activity_main, menu);

		mMainMenu = menu;
		if (mMainMenu != null){
			mMainMenu.findItem(R.id.action_previous).setEnabled(mNbImages > 0);
			mMainMenu.findItem(R.id.action_next).setEnabled(mNbImages > 0);
		}
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
        case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
            return true;
		case R.id.menu_sync:      MainMenu.onMenuSync(this); 	 return true;
		case R.id.menu_settings:  MainMenu.onMenuSettings(this); return true;
		case R.id.menu_help:      MainMenu.onMenuHelp(this);     return true;
		case R.id.menu_about:     MainMenu.onMenuAbout(this);    return true;
		case R.id.action_previous:
			mPager.setCurrentItem((mPager.getCurrentItem() + mNbImages - 1) % mNbImages);
			return true;
		case R.id.action_next:
			mPager.setCurrentItem((mPager.getCurrentItem() + 1) % mNbImages);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}	

	private Uri[] getImagesFiles(Uri uri) {
		Cursor cur = getContentResolver().query(uri, null, null, null, null);

		Uri[] imgs = null;
		if (cur == null) {
			imgs = new Uri[0];
			Toast.makeText(this, R.string.msg_no_data_available, Toast.LENGTH_LONG).show();
		}
		else {
			cur.moveToFirst();

			int size = cur.getCount();
			imgs = new Uri[size];
			for (int i = 0; i < size; i++) {
				imgs[i] = Uri.parse(cur.getString(2));
				cur.moveToNext();
			}
			cur.close();
		}

		return imgs;
	}
	
	private void notifyPagerDataChange() {
		mPagerAdapter.setImages(mImages);
		mPagerAdapter.notifyDataSetChanged();

		if (mMainMenu != null){
			mMainMenu.findItem(R.id.action_previous).setEnabled(mNbImages > 0);
			mMainMenu.findItem(R.id.action_next).setEnabled(mNbImages > 0);
		}
	}

}


